package com.wuzihao.disk.Recycle.controller;

import com.wuzihao.disk.mybatis.flexigrid.PagingEntity;
import com.wuzihao.disk.mybatis.flexigrid.TblResponseEntity;
import com.wuzihao.disk.Recycle.entity.FileEntity;
import com.wuzihao.disk.Recycle.service.RecycleService;
import com.wuzihao.disk.spring.control.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/recycle")
public class RecycleController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(RecycleController.class);

    @Resource
    private RecycleService recycleService;

//    @Secured("ROLE_cldk_recycle_view")
    @RequestMapping("/file")
    public String recyclePage() {
        return "recycle/recyclePage";
    }

//    @Secured("ROLE_cldk_recycle_view")
    @RequestMapping("/list")
    @ResponseBody
    public TblResponseEntity<FileEntity> getRecycleFiles(FileEntity searchEntity, PagingEntity paging) {
        logger.info("回收站查询请求 - 搜索条件: fileName={}", searchEntity.getFileName());
        return recycleService.queryRecycleFiles(searchEntity, paging);
    }

//    @Secured("ROLE_cldk_recycle_restore")
    @RequestMapping("/restore")
    @ResponseBody
    public String restoreFile(@RequestParam("fileId") String fileId) {
        try {
            recycleService.restoreFile(fileId);
            return successJson("recycle.restore.success");
        } catch (Exception e) {
            return failJson("recycle.restore.fail", e.getMessage());
        }
    }

//    @Secured("ROLE_cldk_recycle_delete")
    @RequestMapping("/delete")
    @ResponseBody
    public String deletePermanently(@RequestParam("fileId") String fileId) {
        try {
            recycleService.deletePermanently(fileId);
            return successJson("recycle.delete.success");
        } catch (Exception e) {
            return failJson("recycle.delete.fail", e.getMessage());
        }
    }

//    @Secured("ROLE_cldk_file_delete")
    @RequestMapping("/moveToRecycle")
    @ResponseBody
    public String moveToRecycle(@RequestParam("fileId") String fileId) {
//        try {
            recycleService.moveToRecycle(fileId);
            return successJson("recycle.move.success");
//        } catch (Exception e) {
//            return failJson("recycle.move.fail", e.getMessage());
//        }
    }


}
